<?php

class Admin_IndexController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        #store
        $storeDb = new Admin_Model_Store();
        $enabledStores = sizeof($storeDb->fetchAll("true"));
        $totalStores = sizeof($storeDb->fetchAll());
        $this->view->enabledStores = $enabledStores;
        $this->view->disabledStores = $totalStores - $enabledStores;
        $this->view->allStores = $totalStores;

        #brands
        $brandDb = new Admin_Model_Brand();
        $this->view->enabledBrands = sizeof($brandDb->fetchAll("true"));
        $this->view->disabledBrands = sizeof($brandDb->fetchAll("false"));
        $this->view->allBrands = sizeof($brandDb->fetchAll("full"));

        #feedbacks
        $feedbackDb = new Admin_Model_Feedback();
        $this->view->totalFeedbacks = sizeof($feedbackDb->fetchAll());
        $this->view->todayNewFeedbacks = sizeof($feedbackDb->fetchAll(true));

        #email & sms
        $notifyDb = new Admin_Model_Sentnotifications();
        $outArray = array();
        $outArray['totalEmail'] = 0;
        $outArray['totalSms'] = 0;
        $outArray['totalSmsSuccess'] = 0;
        $outArray['totalSmsFail'] = 0;
        $outArray['totalDeliver'] = 0;
        $outArray['totalOpen'] = 0;
        $outArray['totalClick'] = 0;
        $outArray['totalBounce'] = 0;
        $outArray['totalDropped'] = 0;
        $outArray['totalSpam'] = 0;
        $outArray['totalUnsubscribe'] = 0;
        $notifyAll = $notifyDb->fetchAll();
        foreach ($notifyAll as $notify) {
            if ($this->is_not_empty($notify['email']->email)) {
                $outArray['totalEmail']++;
                $events = $notify['email']->events;
                if ($this->is_not_empty($events->delivered)) {
                    $outArray['totalDeliver']++;
                }
                if ($this->is_not_empty($events->open)) {
                    $outArray['totalOpen']++;
                }
                if ($this->is_not_empty($events->click)) {
                    $outArray['totalClick']++;
                }
                if ($this->is_not_empty($events->bounce)) {
                    $outArray['totalBounce']++;
                }
                if ($this->is_not_empty($events->dropped)) {
                    $outArray['totalDropped']++;
                }
                if ($this->is_not_empty($events->spamreport)) {
                    $outArray['totalSpam']++;
                }
                if ($this->is_not_empty($events->unsubscribe)) {
                    $outArray['totalUnsubscribe']++;
                }
            }
            if ($this->is_not_empty($notify['sms']->mobile)) {
                $outArray['totalSms']++;
                if ($notify['sms']->sent) {
                    $outArray['totalSmsSuccess']++;
                } else {
                    $outArray['totalSmsFail']++;
                }
            }
        }
        $this->view->emailSms = $outArray;
    }

    public function is_not_empty($value) {
        if ($value == "") {
            return false;
        }
        return true;
    }

}

